package com.ktgj.flight.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.FlightAdjustPassenger;
import com.ktgj.flight.domain.vo.FlightAdjustPassengerListVo;
import com.ktgj.flight.mapper.FlightAdjustPassengerMapper;
import com.ktgj.flight.service.IFlightAdjustPassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 机票大客户白名单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-10
 */
@Service
public class FlightAdjustPassengerServiceImpl implements IFlightAdjustPassengerService 
{
    @Autowired
    private FlightAdjustPassengerMapper flightAdjustPassengerMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询机票大客户白名单
     * 
     * @param pId 机票大客户白名单ID
     * @return 机票大客户白名单
     */
    @Override
    public FlightAdjustPassenger selectFlightAdjustPassengerById(Long pId)
    {
        return flightAdjustPassengerMapper.selectFlightAdjustPassengerById(pId);
    }

    /**
     * 查询机票大客户白名单列表
     * 
     * @param flightAdjustPassenger 机票大客户白名单
     * @return 机票大客户白名单
     */
    @Override
    public List<FlightAdjustPassenger> selectFlightAdjustPassengerList(FlightAdjustPassenger flightAdjustPassenger)
    {
        return flightAdjustPassengerMapper.selectFlightAdjustPassengerList(flightAdjustPassenger);
    }

    /**
     * 新增机票大客户白名单
     * 
     * @param flightAdjustPassenger 机票大客户白名单
     * @return 结果
     */
    @Override
    public int insertFlightAdjustPassenger(FlightAdjustPassenger flightAdjustPassenger)
    {
        flightAdjustPassenger.setCreateTime(DateUtils.getNowDate());
        return flightAdjustPassengerMapper.insertFlightAdjustPassenger(flightAdjustPassenger);
    }

    /**
     * 修改机票大客户白名单
     * 
     * @param flightAdjustPassenger 机票大客户白名单
     * @return 结果
     */
    @Override
    public int updateFlightAdjustPassenger(FlightAdjustPassenger flightAdjustPassenger)
    {
        return flightAdjustPassengerMapper.updateFlightAdjustPassenger(flightAdjustPassenger);
    }

    /**
     * 批量删除机票大客户白名单
     * 
     * @param pIds 需要删除的机票大客户白名单ID
     * @return 结果
     */
    @Override
    public int deleteFlightAdjustPassengerByIds(Long[] pIds)
    {
        return flightAdjustPassengerMapper.deleteFlightAdjustPassengerByIds(pIds);
    }

    /**
     * 删除机票大客户白名单信息
     * 
     * @param pId 机票大客户白名单ID
     * @return 结果
     */
    @Override
    public int deleteFlightAdjustPassengerById(Long pId)
    {
        return flightAdjustPassengerMapper.deleteFlightAdjustPassengerById(pId);
    }

    /**
     * 查询待修改的渠道调整大客户列表信息
     * */
    @Override
    public List<FlightAdjustPassenger> flightAdjustPassengerList(Long id) {
        List<FlightAdjustPassenger> flightAdjustPassengerList = flightAdjustPassengerMapper.queryCardId(id);
        return flightAdjustPassengerList;
    }

    /**
     * 修改渠道调整大客户列表信息
     * */
    @Override
    public int updatePassengerVo(HttpServletRequest request, FlightAdjustPassengerListVo flightAdjustPassengerList) throws Exception {
        Date date = new Date();
        String token = request.getHeader("token");
        R<Employee> loginEmployeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);
        if (loginEmployeeByToken.getData() != null){
            for (FlightAdjustPassenger flightAdjustPassenger : flightAdjustPassengerList.getFlightAdjustPassengerList()) {
                flightAdjustPassenger.setEmployeeId(loginEmployeeByToken.getData().getEmployeeId().toString());
                flightAdjustPassenger.setOperateTime(date);
                int i = flightAdjustPassengerMapper.updateFlightAdjustPassenger(flightAdjustPassenger);
                if (i <= 0){
                    throw new Exception("修改失败" + flightAdjustPassenger.getName());
                }
            }
            return 1;
        }
        else {
            throw new Exception(loginEmployeeByToken.getMsg());
        }
    }
}
